#include "doorbell_mqtt.h"

#include "mqtt_client.h"
#include "cJSON.h"
#define TAG "mqtt"

#define BROKER_URI "ws://broker.emqx.io:8083/mqtt"

void        doorbell_mqtt_DataHandle(char *data, int len);
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
    ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32, base, event_id);
    esp_mqtt_event_handle_t  event  = event_data;
    esp_mqtt_client_handle_t client = event->client;
    int                      msg_id;
    switch((esp_mqtt_event_id_t)event_id)
    {
        case MQTT_EVENT_CONNECTED:

            /* 连接成功之后, 执行到这里: 订阅 teacher/cmd */
            ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
            msg_id = esp_mqtt_client_subscribe(client, "a/cmd", 0);
            break;
        case MQTT_EVENT_DISCONNECTED:
            ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
            break;

        case MQTT_EVENT_SUBSCRIBED:
            ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
            break;
        case MQTT_EVENT_UNSUBSCRIBED:
            ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
            break;
        case MQTT_EVENT_PUBLISHED:
            ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
            break;
        case MQTT_EVENT_DATA:
            /* 收到订阅的主题发来的数据 */
            // TODO
            /* ESP_LOGI(TAG, "MQTT_EVENT_DATA");
            printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
            printf("DATA=%.*s\r\n", event->data_len, event->data); */
            doorbell_mqtt_DataHandle(event->data, event->data_len);
            break;
        case MQTT_EVENT_ERROR:
            ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
            break;
        default:
            ESP_LOGI(TAG, "Other event id:%d", event->event_id);
            break;
    }
}

void doorbell_mqtt_Init(void)
{
    const esp_mqtt_client_config_t mqtt_cfg = {
        .broker.address.uri = BROKER_URI,
        //.task.priority = 20,  // mqtt任务的优先级
    };
    /* 初始化mqtt 客户端 */
    esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);

    /* 注册mqtt事件 */
    esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);

    /* 启动mqtt 客户端*/
    esp_mqtt_client_start(client);
}

extern TaskHandle_t mainTask;
void                doorbell_mqtt_DataHandle(char *data, int len)
{
    char buff[len + 1];
    memset(buff, 0, len + 1);
    memcpy(buff, data, len);

    /*  */
    cJSON *obj      = cJSON_Parse(buff);
    cJSON *cmdItem  = cJSON_GetObjectItem(obj, "cmd");
    cJSON *typeItem = cJSON_GetObjectItem(obj, "type");
    cJSON *dirItem  = cJSON_GetObjectItem(obj, "dir");

    char *cmd  = cJSON_GetStringValue(cmdItem);
    char *type = cJSON_GetStringValue(typeItem);
    char *dir  = cJSON_GetStringValue(dirItem);

    if(strcmp(type, "audio") == 0)
    {
        if(strcmp(cmd, "on") == 0)
        {
            if(strcmp(dir, "client2esp") == 0)
            {
                xTaskNotify(mainTask, on_audio_client2esp, eSetValueWithOverwrite);
            }
            else if(strcmp(dir, "esp2client") == 0)
            {
                xTaskNotify(mainTask, on_audio_esp2client, eSetValueWithOverwrite);
            }
        }
        else if(strcmp(cmd, "off") == 0)
        {
            if(strcmp(dir, "client2esp") == 0)
            {
                xTaskNotify(mainTask, off_audio_client2esp, eSetValueWithOverwrite);
            }
            else if(strcmp(dir, "esp2client") == 0)
            {
                xTaskNotify(mainTask, off_audio_esp2client, eSetValueWithOverwrite);
            }
        }
    }
    else if(strcmp(type, "video") == 0)
    {
        if(strcmp(cmd, "on") == 0)
        {
            xTaskNotify(mainTask, on_video_esp2client, eSetValueWithOverwrite);
        }
        else if(strcmp(cmd, "off") == 0)
        {
            xTaskNotify(mainTask, off_video_esp2client, eSetValueWithOverwrite);
        }
    }
    cJSON_free(obj);
}
